/**
 * Created by xiaoz on 16/8/13.
 */
define(["utils","base"],function(utils,baseClass){
    var Component = function(config){
        var _this = this;
        Component.baseConstructor.call(this,config);
        this.$el.addClass("yy-webview");
        var name = config.iframeName || this.config.comKey;
        this.name = name;
        var frameName = new Date().valueOf().toString();

        this.iframe = $("<iframe name='"+frameName+"'></iframe>");
        this.bkCover = $("<div class='yy-webview-bk displaynone'></div>");
        this.$el.append(this.iframe);
        if(this.config.src){
          this.setSrc(this.config.src);
        }
        if(!this.config.delay&&this.config.delay!==0){
            this.delay = 2000;
        }else{
            this.delay = this.config.delay;
        }
        this.bkCover.appendTo(this.$el);
        this.bkCover.spin('tiny', '#999');


        var afterLoad = config.comKey+"_afterload";
        this.afterLoadMethod = this.pageview.plugin[afterLoad];
        window.setTimeout(function(){
          try{
            if(_this.afterLoadMethod){
              _this.afterLoadMethod.call(_this.pageview.plugin,_this,{frame:window.frames[frameName],_document:window.frames[frameName].document,name:frameName});
            }
            $(window.frames[frameName].document.body).addClass(_this.config.comKey);
          }catch(e){

            console.log(e);

          }

        },this.delay);

        this.iframe.bind("load",function(){
          _this.onLoad();
        });

    }

    utils.extendClass(Component,baseClass);


    Component.prototype.setSrc = function(src){
      var _this = this;
      this.bkCover.removeClass("displaynone");
      this.iframe[0].src = src;
    };
    Component.prototype.onLoad = function(){
      var _this = this;
      window.setTimeout(function(){
        _this.bkCover.addClass("displaynone");
      },this.delay);
    }


    return Component;
});
